﻿
/***************************************************************************************
   Date    : January 30, 2006
   Purpose : Implement the  Class DisplayScreen 
   Maintenance Log : 
   Revision		Date			Description 
   --------		----		------------------------------------------------------
	1.0		 07/02/2008		    Initial Release

****************************************************************************************/




using System;

namespace Hclt.Msft.Til.CricketScoreboard
{
    /// <summary>
    /// DisplayScreen class contains logic for 
    /// dispalying screen content 
    /// </summary>
    public static class DisplayScreen
    {  
        /// <summary>
        /// ScreenScene function contains logic to be display the screen
        /// </summary>
        /// <param name="currentScene">
        ///  conatain information about current scene 
        /// </param>
        /// <param name="sceneKey">
        /// parameter used to set  display time
        /// </param>
        /// <param name="defaultValue">
        /// default value in case no value is in configuration file
        /// </param>
        /// <param name="alternateScene">
        ///  which scene to be dispalyed if time has elapsed 
        /// </param>
        /// <returns>
        /// returns  appropriate scene to be shown on the scoreboard 
      
        /// </returns>
        private static ScreenScene GetScreenScene(ScreenScene currentScene, string sceneKey, int defaultValue, ScreenScene alternateScene)
        {
            ScreenScene scene = ScreenScene.None;
            //gets the time limit of current scene
            TimeSpan sceneDisplayTime = Utility.GetSceneDisplayTime(currentScene);
            //gets the configured dispaly time from parameters 
            int configuredDisplayTime = Utility.GetIntFromConfig(sceneKey, defaultValue);
            // check for duration and displays required scene on scoreboard
            if (sceneDisplayTime.TotalSeconds >= configuredDisplayTime)
            {
                scene = alternateScene;
            }
            else
            {
                scene = currentScene;
            }
            return scene;
        }
        /// <summary>
        /// CreateScene function will decide contents of  screen 
        /// w.r.t event occured
        /// </summary>
        /// <param name="currentScore">
        /// takes the current scoreboard object as argument
        /// </param>
        public static void CreateScene(Scoreboard currentScore)
        {   //get current scene from session
            ScreenScene scene = Utility.GetCurrentScene();
                       
            //gets scene to be displayed
            if (scene != ScreenScene.FourHit && currentScore.CheckFourHit())
            {
                scene = ScreenScene.FourHit;
            }
            else if (scene != ScreenScene.SixHit && currentScore.CheckSixHit())
            {
                scene = ScreenScene.SixHit;
            }
            else if (scene != ScreenScene.BatsmanOut && currentScore.CheckWicketFall())
            {
                scene = ScreenScene.BatsmanOut;
            }
            else if (scene != ScreenScene.InningsOver && currentScore.InningsOver)
            {
                scene = ScreenScene.InningsOver;
            }
            else if (scene == ScreenScene.InningsOver && !currentScore.InningsOver)
            {
                scene = ScreenScene.BattingSheet;
            }

            //add previous score to session
            Utility.AddToSession("TeamPreviousScore", currentScore.TeamTotalScore);
            
            //gets current scene to dispalyed based on current  scene and time elapsed
            if (scene == ScreenScene.BattingSheet)
            {  
                scene = GetScreenScene(scene, "BattingSceneDisplayTime", Convert.ToInt32(DefaultDisplayDuration.BattingScene), ScreenScene.BowlingSheet);
            }
            else if (scene == ScreenScene.BowlingSheet)
            {
                scene = GetScreenScene(scene, "BowlingSceneDisplayTime", Convert.ToInt32(DefaultDisplayDuration.BowlingScene), ScreenScene.BattingSheet);
            }
            else if(scene == ScreenScene.BatsmanOut)
            {
                scene = GetScreenScene(scene, "OutSceneDisplayTime", Convert.ToInt32(DefaultDisplayDuration.OutScene), ScreenScene.BattingSheet);
            }
            else if(scene == ScreenScene.FourHit)
            {
                scene = GetScreenScene(scene, "FourHitSceneDisplayTime", Convert.ToInt32(DefaultDisplayDuration.FourHitScene), ScreenScene.BattingSheet);
            }
            else if (scene == ScreenScene.SixHit)
            {
                scene = GetScreenScene(scene, "SixHitSceneDisplayTime", Convert.ToInt32(DefaultDisplayDuration.SixHitScene), ScreenScene.BattingSheet);
            }
            else if (scene == ScreenScene.None)
            {
                scene = ScreenScene.BattingSheet;
            }

            int counter;
            ScoreSheetXaml sheetDisplay = new ScoreSheetXaml(scene);
            //batting sheet is dispalyed
            if (scene == ScreenScene.BattingSheet)
            {
                // poplulate various fields  in batting sheet by updating  XAML file 
                sheetDisplay.UpdateXaml("RectTeamName", string.Concat(currentScore.BattingTeamName," Vs ",currentScore.FieldingTeamName));
                sheetDisplay.UpdateXaml("tbTeamBating", currentScore.BattingTeamName);
                sheetDisplay.UpdateXaml("tbTournament", Utility.GetStringFromConfig("TournamentName", "TournamentName"));
                BatsmanInfoCollection batsmanList = currentScore.BatsmanDetails;
                int playerCount = batsmanList.Count;
                // populates entry for batsman
                for (int i = 0; i < playerCount; i++)
                {
                   counter = i + 1;
                   sheetDisplay.UpdateXaml(string.Concat("tbP", counter, "Name"), batsmanList[i].BatsmanName);
                   sheetDisplay.UpdateGradientColor(string.Concat("path_name_", counter), batsmanList[i].Status == BatsmanStatus.Playing, true);
                   sheetDisplay.UpdateGradientColor(string.Concat("path_Details_", counter), batsmanList[i].Status == BatsmanStatus.Playing, false);
                    if (batsmanList[i].Status != BatsmanStatus.Dnb)
                    {
                        sheetDisplay.UpdateXaml(string.Concat("tbP", counter, "Desc"), batsmanList[i].StatusDescription);
                        sheetDisplay.UpdateXaml(string.Concat("tbP", counter, "Runs"), batsmanList[i].BatsmanScore);
                        sheetDisplay.UpdateXaml(string.Concat("tbP", counter, "Balls"), batsmanList[i].TotalBallsPlayed);
                        sheetDisplay.UpdateXaml(string.Concat("tbP", counter, "Fours"), batsmanList[i].TotalFours);
                        sheetDisplay.UpdateXaml(string.Concat("tbP", counter, "Sixs"), batsmanList[i].TotalSixes);
                        sheetDisplay.UpdateXaml(string.Concat("tbP", counter, "SR"), batsmanList[i].StrikeRate);
                    }
                }
                //extras  being populated in xaml file 
                sheetDisplay.UpdateXaml("tbExtras", currentScore.TotalExtras);
                sheetDisplay.UpdateXaml("tbBye", currentScore.ExtrasDetails.TotalByes);
                sheetDisplay.UpdateXaml("tbNo", currentScore.ExtrasDetails.TotalNoBalls);
                sheetDisplay.UpdateXaml("tbWide", currentScore.ExtrasDetails.TotalWideBalls);
                sheetDisplay.UpdateXaml("tbTotalScore", string.Concat(currentScore.TeamTotalScore, "/", currentScore.TotalWicketsDown));
               //total overs populated in xaml file
               sheetDisplay.UpdateXaml("tbTotalOvers", currentScore.TotalOvers);
            }
                 //bowling sheet is dispalyed
               else if(scene == ScreenScene.BowlingSheet)
            {
               
               // poplulate various fields  in bowling sheet by updating  XAML file 
                sheetDisplay.UpdateXaml("RectTeamName", string.Concat(currentScore.BattingTeamName, " Vs ", currentScore.FieldingTeamName));
                sheetDisplay.UpdateXaml("tbTournament", Utility.GetStringFromConfig("TournamentName", "TournamentName"));
                sheetDisplay.UpdateXaml("tbTeamBowling", currentScore.FieldingTeamName);

                BowlerInfoCollection bowlerList = currentScore.BowlerDetails;

                int playerCount = bowlerList.Count;
                   
                // populates entry for bowler
                for (int i = 0; i < playerCount; i++)
                {
                    counter = i + 1;

                    sheetDisplay.UpdateXaml(string.Concat("tbP", counter, "Name"), bowlerList[i].BowlerName);
                    sheetDisplay.UpdateGradientColor(string.Concat("path_name_", counter), bowlerList[i].Status == BowlerStatus.Bowling, true);
                    sheetDisplay.UpdateGradientColor(string.Concat("path_Details_", counter), bowlerList[i].Status == BowlerStatus.Bowling, false);

                    if (bowlerList[i].TotalBalls > 0)
                    {
                        sheetDisplay.UpdateXaml(string.Concat("tbP", counter, "Overs"), bowlerList[i].TotalOvers);
                        sheetDisplay.UpdateXaml(string.Concat("tbP", counter, "Runs"), bowlerList[i].TotalRuns);
                        sheetDisplay.UpdateXaml(string.Concat("tbP", counter, "Wickets"), bowlerList[i].TotalWickets);
                        sheetDisplay.UpdateXaml(string.Concat("tbP", counter, "Rpo"), bowlerList[i].RunsPerOver);
                    }
                }
                   //populates extra's
                sheetDisplay.UpdateXaml("tbExtras", currentScore.TotalExtras);
                sheetDisplay.UpdateXaml("tbBye", currentScore.ExtrasDetails.TotalByes);
                sheetDisplay.UpdateXaml("tbNo", currentScore.ExtrasDetails.TotalNoBalls);
                sheetDisplay.UpdateXaml("tbWide", currentScore.ExtrasDetails.TotalWideBalls);
             
            }

           //displays match summary
            else if(scene == ScreenScene.InningsOver)
            {
                sheetDisplay.UpdateXaml("tbMessage", currentScore.MatchSummary);
            }

            
            sheetDisplay.WriteXaml();
            
            
            //scene is added to session
            Utility.AddSceneToSession(scene);

        }

    }

}
